﻿using System;
using Microsoft.SharePoint;
using ServBus.ListForm2013.Utility;
using InfoJetSoft.Service.Util;

namespace ServBus.ListForm2013.Extension
{
    public class ResourceFileStorage : IResourceFileStorage
    {
        private SPWeb _web;
        private SPList _list;
        private string _urlPrefix;

        public ResourceFileStorage(SPWeb web)
        {
            _web = web;
            if (web.Properties.ContainsKey(Constants.ATT_LIB_KEY))
            {
                try
                {
                    Guid doclibId = new Guid(web.Properties[Constants.ATT_LIB_KEY]);
                    _list = _web.Lists[doclibId];
                    _urlPrefix = _list.RootFolder.ServerRelativeUrl;
                }
                catch (Exception ex)
                {
                    ErrorLog.Write(ex.Message + Environment.NewLine + ex.StackTrace,
                        System.Diagnostics.EventLogEntryType.Error);
                }
            }
        }

        #region IResourceFileStorage 成员

        public void Cache(string templateId, string fileName, byte[] content)
        {
            try
            {
                string folderUrl = _urlPrefix + "/" + Constants.XSN_CACHE_PREFIX + templateId;
                SPFolder folder = _web.GetFolder(folderUrl);
                if (!folder.Exists)
                {
                    _web.AllowUnsafeUpdates = true;
                    folder = _list.RootFolder.SubFolders.Add(Constants.XSN_CACHE_PREFIX + templateId);
                }
                _web.AllowUnsafeUpdates = true;
                folder.Files.Add(fileName, content, true);
            }
            catch (Exception ex)
            {
                ErrorLog.Write("Cache File Failed:" + fileName + Environment.NewLine +
                    "TemplateId:" + templateId + Environment.NewLine +
                    ex.Message + Environment.NewLine + ex.StackTrace,
                     System.Diagnostics.EventLogEntryType.Error);
            }
        }

        public string GetResourceFileUrl(string templateId, string fileName)
        {
            return _urlPrefix + "/" + Constants.XSN_CACHE_PREFIX + templateId + "/" + fileName;
        }

        public byte[] Resource(string fileUrl)
        {
            //int idIndex = fileUrl.IndexOf("?id=");
            //int nameIndex = fileUrl.IndexOf("&name=");
            //if (idIndex > 0 && nameIndex > 0 && nameIndex > idIndex)
            //{
            //    string templateId = fileUrl.Substring(idIndex + 4, nameIndex - idIndex - 4);
            //    string fileName = fileUrl.Substring(nameIndex + 6);

            //    //CID 0988 //2011.07.17 //Begin
            //    fileName = HttpUtility.UrlDecode(fileName);
            //    //CID 0988 //2011.07.17 //End

            //    string filePath = @"c:\log\" + templateId + @"\" + fileName;
            //    FileStream stream = new FileStream(filePath, FileMode.Open);
            //    byte[] content = new byte[stream.Length];
            //    stream.Read(content, 0, content.Length);
            //    stream.Close();
            //    return content;
            //}
            //else
            //{
            //    return null;
            //}
            return null;
        }

        #endregion
    }
}
